package com.hjl.dao.impl;

import com.hjl.Utils.DBUtil;
import com.hjl.dao.UserDao;
import com.hjl.entity.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ServivE
 * @version 0.1
 * @className UserDaoImpl
 * @description
 * @date 2024/7/23 15:27
 * @since jdk11
 */
public class UserDaoImpl extends DBUtil implements UserDao {

    public List<User> testQuery() {
        //获取连接
        Connection connection = getConnection();
        String sql = "select * from t_user";
        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            ResultSet resultSet = preparedStatement.executeQuery();
            List<User> list = new ArrayList<User>();
            while (resultSet.next()) {
                User user = new User();
                user.setId(resultSet.getInt("id"));
                user.setAccount(resultSet.getString("account"));
                user.setPASSWORD(resultSet.getString("password"));
                user.setNickname(resultSet.getString("nickname"));
                list.add(user);
            }
            closeAll(resultSet, preparedStatement, connection);
            return list;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
